let dragons = require('dragons');
let soundMgr = require('soundMgr');
cc.Class({
    extends: cc.Component,

    properties: {
        helloVoice:'',
        helloAnimation:'Speak',

        idleAnimation:'Idle',
        eatAnimation:'Eat',
        countAnimation:'Count',
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

    },

    start () {

    },
    /**
     * 打招呼
     * @param callback
     */
    sayHello(callback){
        let self = this;
        let voiceId = self.helloVoice;
        if (!!voiceId){
            soundMgr.playSoundEffect(voiceId);
        }

        if (self.helloAnimation){
            dragons.playAnimation(self.node,self.helloAnimation,1,function () {
                if (callback){
                    callback();
                }
            });
        }
    },
    idle(){
        let self = this;
        if (self.idleAnimation){
            dragons.playAnimation(self.node,self.idleAnimation,0);
        }
    },
    /**
     * 数东西
     * @param nodes
     * @param complete
     */
    countSomethings(complete){
        let self = this;
        if (self.countAnimation){
            dragons.playAnimation(self.node,self.countAnimation,1,function () {
                if (complete){
                    complete();
                }
            });
        }
    },
    /**
     * 吃东西
     * @param nodes
     * @param complete
     */
    eatSomethings(nodes,complete){
        let self = this;
        if (self.idleAnimation){
            self._eatByIndex(nodes,0,complete);
        }
    },
    _eatByIndex(nodes,index,complete){
        let self = this;
        if (index < nodes.length){
            let node = nodes[index];
            let delay = cc.delayTime(0.4);
            let callFun = cc.callFunc(function () {
                node.active = false;
            });
            node.runAction(cc.sequence(delay,callFun))
        }
        index++;
        dragons.playAnimation(self.node,self.eatAnimation,1,function () {
            if (index<nodes.length){
                self._eatByIndex(nodes,index,complete);
            }else{
                complete();
            }
        });
    }
    // update (dt) {},
});
